﻿Public Class CasesV3Form

    Dim TheData As DataView
    Dim TheTable As New DataTable
    Dim TheDataSource As New DataView

    Private Sub CasesV3Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProviderBox.zConnectionString = StoreConnect

        ThePeriodBox.zConnectionString = StoreConnect

        ThePeriodBox.zSelectedPeriod_ID = CurrentPeriodID


    End Sub


    Private Sub LoadCaseBox()

        Dim SQL As String
        Dim TheData As DataView

        If CurrentUser.Level = 5 Then

            SQL = "Select DataCut_ID, Description from Modelling_Case_Datasets where Period_ID = '" & ThePeriodBox.zSelectedPeriod_ID & "' Order By Description "

        Else

            SQL = "Select DataCut_ID, Description from Modelling_Case_Datasets where Period_ID = '" & ThePeriodBox.zSelectedPeriod_ID & "' and Invisible = 0 Order By Description "

        End If


        TheData = LoadDataView(SQL)

        'If there are no cases then tell the user by updating the casesbox
        If TheData.Count = 0 Then
            SQL = "Select 'None' as DataCut_ID, 'No cases exist for the period " & ThePeriodBox.zSelectedPeriod_Name & "' as Description"
        End If

        If LoadComboBox(CaseBox, SQL, "DataCut_ID", "Description") = False Then
            MessageBox.Show("There has been an error loading the cases, please contact The Store administrator", "Cases Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If


    End Sub


    Private Sub ThePeriodBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThePeriodBox.SelectedIndexChanged
        LoadCaseBox()

       

    End Sub

    Private Sub ViewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub

    Private Sub ViewTheCase(Optional ByVal AllProviders As Boolean = False)

        TheData = Nothing

        If AllProviders = False Then
            If ProviderBox.zSelectedProvider.RID = "" Then
                MessageBox.Show("No provider has been selected", "Select provider", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                Exit Sub
            End If

        End If
        
        Dim TheRID As String

        If AllProviders = True Then
            TheRID = ""
        Else
            TheRID = ProviderBox.zSelectedProvider.RID
        End If

        Dim Params As New List(Of Object)

        Params.Clear()

        Params.Add(CaseBox.SelectedValue) 'datacut_id
        Params.Add(ThePeriodBox.zSelectedPeriod_ID) 'period id
        Params.Add(TheRID) 'RID


        If DoSomeWork("Cases", Params) = Windows.Forms.DialogResult.OK Then
            If BusyButFine.ReturnedValues.Count <> 0 Then


                TheData = TryCast(BusyButFine.ReturnedValues(0), DataView)
                
                If Not TheData Is Nothing Then

                    If AllProviders = False Then
                        CaseHeaderLabel.Text = ThePeriodBox.zSelectedPeriod_Name & " - " & TryCast(CaseBox.SelectedItem, DataRowView)("Description").ToString

                        ProviderHeaderLabel.Text = ProviderBox.zSelectedProvider.ProviderName & " (" & ProviderBox.zSelectedProvider.UPIN & ")"

                        LoadRepeater()
                        GridPanel.Visible = False
                        ProviderPanel.Dock = DockStyle.Fill
                        ProviderPanel.Visible = True
                    Else
                        TheGrid.DataSource = TheData

                        LoadGrid()

                        ProviderPanel.Visible = False
                        GridPanel.Dock = DockStyle.Fill
                        GridPanel.Visible = True


                    End If
                   
                End If

            End If
        End If
    End Sub

    Private Sub LoadGrid()
        Dim c As DataGridViewColumn

        For Each c In TheGrid.Columns
            Dim ColName As String = c.Name



            If ColName.Contains("STATUS_") Then
                c.Visible = False
            Else
                c.HeaderText = c.HeaderText.Replace("PRODUCT_", "")
                c.HeaderText = c.HeaderText.Replace("INPUT_", "")
                c.HeaderText = c.HeaderText.Replace("APPROVED_", "")
                c.HeaderText = c.HeaderText.Replace("FORMULA_", "")
            End If


            Select Case ColName
                Case "RID"
                    c.Visible = False

                Case Else

            End Select


        Next
    End Sub

    Private Sub LoadRepeater()


        TheTable.Clear()
        TheTable.Columns.Clear()


        TheTable.Columns.Add("Field_Code", GetType(String))
        TheTable.Columns.Add("Field_Description", GetType(String))
        TheTable.Columns.Add("Value", GetType(String))
        TheTable.Columns.Add("Status", GetType(String))
        TheTable.Columns.Add("RowStatus", GetType(String))

        Dim c As DataColumn
        Dim Status As String = ""
        Dim FieldCode As String = ""

        For Each c In TheData.ToTable.Columns

            If Status <> "" Then
                'the previous column was the status
                TheTable.Rows.Add(FieldCode, Mid(c.ColumnName, 7, 255), TheData(0)(c.ColumnName), Status, "O")
                Status = ""
                FieldCode = ""
            End If

            If c.ColumnName.Contains("PRODUCT_") Then

                TheTable.Rows.Add("PRODUCT", Mid(c.ColumnName, 9, 255), TheData(0)(c.ColumnName), "", "O")
                Status = ""
                FieldCode = ""

            End If

            If c.ColumnName.Contains("STATUS_") Then

                Status = Trim(IIf(IsDBNull(TheData(0)(c.ColumnName)), "Unsupported", TheData(0)(c.ColumnName)))
                FieldCode = Mid(c.ColumnName, 8, 255)

            End If

            If c.ColumnName.Contains("APPROVED_") Then
                TheTable.Rows.Add("APPROVED", Mid(c.ColumnName, 10, 255), TheData(0)(c.ColumnName), "", "O")
                Status = ""
                FieldCode = ""
            End If

        Next

        TheDataSource = New DataView(TheTable)

        'FieldLabel.DataBindings.Clear()
        'FieldLabel.DataBindings.Add(New System.Windows.Forms.Binding("Text", TheDataSource, "Field_Description", True))

        'ValueBox.DataBindings.Clear()
        'ValueBox.DataBindings.Add(New System.Windows.Forms.Binding("Text", TheDataSource, "Value", True))

        'Repeater.DataSource = TheDataSource
        Repeater.ItemCount = TheDataSource.Count
        Repeater.BeginResetItemTemplate()
        Repeater.EndResetItemTemplate()


    End Sub

   

    Private Sub ProviderBox_ProviderSelected(ByVal e As TheStore.ProviderSearchBoxCtrl.SelectedProviderClass) Handles ProviderBox.ProviderSelected
        ViewTheCase()
    End Sub

   
    Private Sub CasesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CasesButton.Click
        CasesMenu.Show(CasesButton, New System.Drawing.Point(0, CasesButton.Height))
    End Sub

    Private Sub CaseBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CaseBox.SelectedIndexChanged
        If ProviderBox.zSelectedProvider.RID <> "" Then
            ViewTheCase()
        End If
    End Sub

    Private Sub ViewDatasetAsGridToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewDatasetAsGridToolStripMenuItem.Click
        ViewTheCase(True)
    End Sub

    Private Sub TheGrid_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles TheGrid.CellFormatting
        Dim colname As String

        colname = TheGrid.Columns(e.ColumnIndex).Name


        If colname.Contains("INPUT_") Then
            Dim Status As String
            Status = Trim(IIf(IsDBNull(TheGrid.Item(e.ColumnIndex - 1, e.RowIndex).Value), "Unsupported", TheGrid.Item(e.ColumnIndex - 1, e.RowIndex).Value))


            Select Case Status
                Case "Approved"
                    e.CellStyle.BackColor = Color.Green
                    e.CellStyle.ForeColor = Color.White
                Case "Rejected"
                    e.CellStyle.BackColor = Color.Red
                    e.CellStyle.ForeColor = Color.White
                Case "Supported"
                    e.CellStyle.BackColor = Color.Orange

                Case Else

            End Select

        End If

    End Sub

    Private Sub Repeater_ItemValueNeeded(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) Handles Repeater.ItemValueNeeded

        If e.ItemIndex < 0 Then Exit Sub

        Dim value As Decimal

        If IsDBNull(TheDataSource(e.ItemIndex)("Value")) Then
            value = 0
        Else
            value = TheDataSource(e.ItemIndex)("Value")
        End If

        Select Case e.Control.Name
            Case "FieldLabel"
                e.Value = TheDataSource(e.ItemIndex)("Field_Description").ToString
            Case "ValueBox"
                e.Value = FormatNumber(value)
        End Select

       
        
    End Sub

    
   
    Private Sub Repeater_DrawItem(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles Repeater.DrawItem

        Dim i As Integer

        i = e.DataRepeaterItem.ItemIndex

        If i < 0 Then Exit Sub

        Dim vbox As TextBox

        vbox = DirectCast(e.DataRepeaterItem.Controls("ValueBox"), TextBox)

        Select Case TheDataSource(i)("Status").ToString
            Case "Approved"
                e.DataRepeaterItem.BackColor = Color.Green
                e.DataRepeaterItem.Controls("FieldLabel").ForeColor = Color.White
                vbox.Enabled = True
                vbox.BorderStyle = BorderStyle.Fixed3D
            Case "Rejected"
                e.DataRepeaterItem.BackColor = Color.Red
                e.DataRepeaterItem.Controls("FieldLabel").ForeColor = Color.White
                vbox.Enabled = True
                vbox.BorderStyle = BorderStyle.Fixed3D
            Case "Supported"
                e.DataRepeaterItem.BackColor = Color.Orange
                e.DataRepeaterItem.Controls("FieldLabel").ForeColor = Color.Black
                vbox.Enabled = True
                vbox.BorderStyle = BorderStyle.Fixed3D
            Case "Unsupported"
                e.DataRepeaterItem.BackColor = Color.White
                e.DataRepeaterItem.Controls("FieldLabel").ForeColor = Color.Black
                vbox.Enabled = True
                vbox.BorderStyle = BorderStyle.Fixed3D
            Case Else
                e.DataRepeaterItem.BackColor = Color.White
                e.DataRepeaterItem.Controls("FieldLabel").ForeColor = Color.Black
                vbox.Enabled = False
                vbox.BorderStyle = BorderStyle.FixedSingle


        End Select
    End Sub

    Private Sub StatusMenu_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles StatusMenu.Opening
        Dim i As Integer

        i = Repeater.CurrentItemIndex

        If UserFormCheck("NewCasesForm") <> "W" Then
            e.Cancel = True
            Exit Sub
        End If

        If i < 0 Then Exit Sub


        Select Case TheDataSource(i)("Status")
            Case "Approved"
                ApproveToolstripMenuItem.Visible = False

                If CurrentUser.Level <> 5 Then

                    UnsupportToolStripMenuItem.Visible = True
                    RejectToolStripMenuItem.Visible = False
                Else
                    UnsupportToolStripMenuItem.Visible = False
                    RejectToolStripMenuItem.Visible = True
                End If

                SupportToolStripMenuItem.Visible = True

            Case "Supported"
                If CurrentUser.Level = 5 Then
                    ApproveToolstripMenuItem.Visible = True
                    RejectToolStripMenuItem.Visible = True
                    UnsupportToolStripMenuItem.Visible = False
                Else
                    ApproveToolstripMenuItem.Visible = False
                    RejectToolStripMenuItem.Visible = False
                    UnsupportToolStripMenuItem.Visible = True
                End If

                SupportToolStripMenuItem.Visible = False


            Case "Unsupported"
                If CurrentUser.Level <> 5 Then
                    SupportToolStripMenuItem.Visible = True
                Else
                    SupportToolStripMenuItem.Visible = False
                End If

                ApproveToolstripMenuItem.Visible = False

                UnsupportToolStripMenuItem.Visible = False
                RejectToolStripMenuItem.Visible = False
            Case "Rejected"
                If CurrentUser.Level = 5 Then
                    ApproveToolstripMenuItem.Visible = True
                    UnsupportToolStripMenuItem.Visible = False
                Else
                    ApproveToolstripMenuItem.Visible = False
                    UnsupportToolStripMenuItem.Visible = True
                End If

                SupportToolStripMenuItem.Visible = True
                RejectToolStripMenuItem.Visible = False

            Case Else
                e.Cancel = True
        End Select


       

    End Sub

    Private Sub TheGrid_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TheGrid.CellDoubleClick
        If e.RowIndex < 0 Then Exit Sub


        ProviderBox.SetProviderByRID(TheGrid.Item("RID", e.RowIndex).Value)

    End Sub

    Private Sub ApproveToolstripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApproveToolstripMenuItem.Click
        SetStatus("Approved")
    End Sub

    Private Sub SetStatus(ByVal Status As String)
        If Repeater.CurrentItemIndex < 0 Then Exit Sub

        Dim i As Integer

        i = Repeater.CurrentItemIndex

        TheDataSource(i)("Status") = Status
        TheDataSource(i)("RowStatus") = "E"

        Repeater.BeginResetItemTemplate()
        Repeater.EndResetItemTemplate()

    End Sub

    Private Sub SupportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SupportToolStripMenuItem.Click
        SetStatus("Supported")
    End Sub

    Private Sub UnsupportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnsupportToolStripMenuItem.Click
        SetStatus("Unsupported")
    End Sub

    Private Sub RejectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RejectToolStripMenuItem.Click
        SetStatus("Rejected")
    End Sub

    Private Sub Repeater_ItemValuePushed(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) Handles Repeater.ItemValuePushed
        Dim i As Integer

        i = e.ItemIndex

        If i < 0 Then Exit Sub


        Select Case e.Control.Name
            Case "ValueBox"

                Try
                    TheDataSource(i)("Value") = e.Value
                    TheDataSource(i)("RowStatus") = "E"
                Catch ex As Exception
                    ' MessageBox.Show("The value you entered is not valid, please try again", "Value not valid", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Try

            Case Else

        End Select

    End Sub
End Class